import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:flutter_2b_client/apis/AuthrizationApi.dart';
import 'package:flutter_2b_client/components/LoadingButton.dart';
import 'package:flutter_hooks/flutter_hooks.dart';
import 'package:fluttertoast/fluttertoast.dart';

import '../theme/custom_color.dart';
import 'LogInPage.dart';

class UpdatePasswordPage extends HookWidget {
  UpdatePasswordPage({Key? key}) : super(key: key);
  static String path = "/updatePassword";
  final _formKey = GlobalKey<FormState>();

  @override
  Widget build(BuildContext context) {
    var oldPasswordState = useState("");
    var passwordState = useState("");
    var confirmPasswordState = useState("");

    return Scaffold(
      appBar: AppBar(
        title: const Text("修改密码"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16),
        child: Form(
          key: _formKey,
          child: Column(
            children: [
              TextFormField(
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return "请输入密码";
                  }
                  return null;
                },
                onChanged: (value) {
                  oldPasswordState.value = value;
                },
                decoration: const InputDecoration(label: Text("原密码")),
                keyboardType: TextInputType.visiblePassword,
                obscureText: true,
              ),
              TextFormField(
                validator: passwordValidator,
                onChanged: (value) {
                  passwordState.value = value;
                },
                decoration: const InputDecoration(label: Text("新密码")),
                keyboardType: TextInputType.visiblePassword,
                obscureText: true,
              ),
              TextFormField(
                validator: (value) {
                  if (value == null || value.isEmpty) {
                    return "请确认密码";
                  } else if (value != passwordState.value) {
                    return "两次输入密码不一致";
                  }
                  return null;
                },
                onChanged: (value) {
                  confirmPasswordState.value = value;
                },
                decoration: const InputDecoration(label: Text("确认密码")),
                keyboardType: TextInputType.visiblePassword,
                obscureText: true,
              ),
              SizedBox(
                width: double.infinity,
                child: Padding(
                  padding: const EdgeInsets.only(top: 8.0),
                  child: LoadingButton(
                    onPressed: () async {
                      if (_formKey.currentState!.validate()) {
                        try {
                          await authorizationApi.updatePassword(
                                                    UpdatePasswordRequest(
                                                      password: oldPasswordState.value,
                                                      newPassword: passwordState.value,
                                                    ),
                                                  );
                          if (context.mounted) {
                                                    ScaffoldMessenger.of(context).showSnackBar(
                                                        const SnackBar(content: Text("修改成功!")));
                                                    Navigator.of(context).pop();
                                                  }
                        } on DioException catch (e) {
                          var response = e.response;
                          if(response?.statusCode==400){
                            Fluttertoast.showToast(msg: "原密码不正确");
                          }
                        }
                      }
                    },
                    child: const Text("提交"),
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
    );
  }
}
